<?php

$max_shift = MAX_SHIFT_LEN;

$sql  = "select * , day(from_unixtime(`start`)) as day, ";
$sql .= "month(from_unixtime(`start`)) as month, ";
$sql .= "year(from_unixtime(`start`)) as year,  ";
$sql .= "(stop - start)/3600 as hours, ";
$sql .= "from_unixtime(`start`) as `Start Time`, ";
$sql .= "from_unixtime(`stop`) as `Stop Time` ";
$sql .= "from need ";
$sql .= "where processed = 0 ";
$sql .= "and (stop - start)/3600 <= $max_shift ";
$sql .= "order by (stop - start)/3600 asc ";

$results = lomgetit($sql);
simple_table($results);

foreach ($results as $r){
	$start = $r['start'];
	$stop = $r['stop'];
	$avail = get_avail($start, $stop);

	if (count($avail) == 0){print "<h1>ERROR: Not Enough Avail!</h1>"; die();}
	foreach ($avail as $a){
		//enough hours?
		$hours = get_employee_sked_hours($a['employee']);
                $shift_len = ($r['stop'] - $r['start'])/3600;
                $total_hours = $hours  + $shift_len;
                print "\n\n\n\nPrev hours for emp ".$hours."\n";
                print "Shift Len: ".$shift_len."\n";
                print "total hours for emp".$total_hours."\n\n\n\n";
                if ($hours + $shift_len > MAX_HOURS_WEEK){
                	//print "<h1>!!over!!</h1>"; die;
                        continue;}

                //verify employee has days left that week

                $days =  get_employee_day_count($a['employee']);
                if ($days == MAX_DAYS_PER_WEEK){print "<h1>Too many days</h1>\n"; continue;}

		//sked employee
		//insert into sked table
                        $skedt = sked_shift($r['start'], $r['stop'], $a['employee'], $a['auto'], $r['auto']);
			break;


	}//foreach ($avail as $a)

}//foreach ($results as $r)
